package org.windey.applet;


import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;

import javax.swing.JApplet;


public class WindeyApplet extends JApplet implements Runnable, KeyListener, MouseListener, MouseMotionListener
{
    /**
	 * 
	 */
	private static final long serialVersionUID = 9214157825869872098L;
	private int hoogte, breedte ;
	private WindeyServerConnection connection;
	private String key ;
	private StreamThread streamThread;
	private BufferedImage image;
    
	public void init()
	{
		final int port = Integer.parseInt(getParameter("port")) ;
		final String hostname = getParameter("hostname") ;
		key = getParameter("key") ;
		
        //effe de size voor de applet apart te runnen zette


        Dimension appletSize = this.getSize();
        hoogte = appletSize.height;
        breedte = appletSize.width;
        setSize(breedte,hoogte) ;
        //setBackground(Color.darkGray) ;
        image = new BufferedImage(breedte, hoogte, BufferedImage.TYPE_BYTE_GRAY);
        
        addMouseListener(this);
        addMouseMotionListener(this);
        addKeyListener(this);
        connection = new WindeyServerConnection(hostname, port);
        setFocusable(true) ;
    }
	
	@Override
	public void start() {
		{
			try
			{
				connection.connect();
				connection.send(key);
				streamThread = new StreamThread(connection, this);
				streamThread.start() ;
			}
			catch (WindeyServerConnectionException e)
			{
				e.printStackTrace();
			}
		}
	}
	
	@Override
	public void stop() {
		{
			try
			{
				if (connection.isConnected())
				{
					connection.send("EOC");
					connection.disconnect();
				}
			}
			catch (WindeyServerConnectionException e)
			{
				e.printStackTrace();
				//showErrorMessage("Unable to disconnect");
			}
		}
	}
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		
	}
	
	public void mouseEntered( MouseEvent e ) {
	      // called when the pointer enters the applet's rectangular area
	}
	
	public void mouseExited( MouseEvent e ) {
	      // called when the pointer leaves the applet's rectangular area
	}
	
	public void mouseClicked( MouseEvent e ) {
	      // called after a press and release of a mouse button
	      // with no motion in between
	      // (If the user presses, drags, and then releases, there will be
	      // no click event generated.)
		connection.send("CLICK:"+e.getX()*2+","+e.getY()*2+","+e.getButton());
	      e.consume();
	}
	
	public void mousePressed( MouseEvent e ) {  // called after a button is pressed down
	      //System.out.println("pressed button down") ;
	      repaint();
	      // "Consume" the event so it won't be processed in the
	      // default manner by the source which generated it.
	      e.consume();
	}
	   
	public void mouseReleased( MouseEvent e ) {  // called after a button is released
	      //System.out.println("released button") ;
	      repaint();
	      e.consume();
	}
	  
	public void mouseMoved( MouseEvent e ) {  // called during motion when no buttons are down
	      //showStatus( "Mouse at (" + mouseX + "," + mouseY + ")" );
	      //System.out.println("beweeg muis, geen knop ingedruk") ;
	      connection.send("MOVE:"+e.getX()*2+","+e.getY()*2);
	      repaint();
	      e.consume();
	}
	  
	public void mouseDragged( MouseEvent e ) {  // called during motion with buttons down
	      //showStatus( "Mouse at (" + mouseX + "," + mouseY + ")" );
	      //System.out.println("beweeg muis, knop ingedruk") ;
	      repaint();
	      e.consume();
	}

	public void paint( Graphics g )
	{
		g.drawImage(image, 0, 0, null);
	}
	
	public void setPixel(int x, int y, int color)
	{
		image.setRGB(x, y, color);
	}

	@Override
	public void keyPressed(KeyEvent e) {
		connection.send("KEY:" + e.getKeyCode());
		System.out.println("key gedrukt: " + e.getKeyChar()) ;
		e.consume();
		
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	
	public void keyTyped(KeyEvent e) {
		connection.send("KEY:" + e.getKeyCode());
		System.out.println("key gedrukt: " + e.getKeyChar()) ;
		e.consume() ;
	}

	public void setImage(BufferedImage image)
	{
		this.image = image;
	}
}
